package com.coinworld.utils;


import com.alibaba.fastjson.JSONObject;
import com.coinworld.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * @description
 * @author:
 * @create: 2019-08-30 14:05
 **/
@Slf4j
public class AESUtils {

    private static final String PASS_WORD = "FENG@MI&LV@XING#";

    private static final String ALGORITHM = "AES/ECB/PKCS5Padding";

    /*****************************************************
     * AES加密
     * @param content 加密内容
     * @param key 加密密码，由字母或数字组成
    此方法使用AES-128-ECB加密模式，key需要为16位
    加密解密key必须相同，如：abcd1234abcd1234
     * @return 加密密文
     ****************************************************/

    public static String enCode(String content, String key) {
        if (key == null || "".equals(key)) {
            return null;
        }
        if (key.length() != 16) {
            return null;
        }
        try {
            byte[] raw = key.getBytes();
            SecretKeySpec skey = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            byte[] byte_content = content.getBytes("utf-8");
            byte[] encode_content = cipher.doFinal(byte_content);
            return Base64.encodeBase64String(encode_content);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*****************************************************
     * AES解密
     * @param content 加密密文
     * @param key 加密密码,由字母或数字组成
    此方法使用AES-128-ECB加密模式，key需要为16位
    加密解密key必须相同
     * @return 解密明文
     ****************************************************/

    public static String deCode(String content, String key) {
        if (key == null || "".equals(key)) {
            return null;
        }
        if (key.length() != 16) {
            return null;
        }
        try {
            byte[] raw = key.getBytes();
            SecretKeySpec skey = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, skey);
            byte[] encode_content = Base64.decodeBase64(content);
            byte[] byte_content = cipher.doFinal(encode_content);
            return new String(byte_content, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @description AES 加密操作
     * @author
     * @date 2019/8/30
     */
    public static String encrypt(String content) {
        return enCode(content, PASS_WORD);
    }


    /**
     * @description 解密操作
     * @author
     * @date 2019/8/30
     */
    public static String decrypt(String content) {
        String data = deCode(content, PASS_WORD);
        if (PubTools.checkXSSAndSql(data)){
            throw new BusinessException("您所访问的页面请求中有违反安全规则元素存在，拒绝访问");
        }
        return data;
    }


    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","张三");
        String s = jsonObject.toJSONString();
        System.out.println("加密前param:" + s);
        String s1 = AESUtils.encrypt(s);
        System.out.println("加密后param:" + s1);
        System.out.println("解密后param:" + AESUtils.decrypt(s1));
    }

}
